{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from keras.utils import np_utils\n",
    "from keras.models import Sequential,model_from_json\n",
    "from keras.utils.vis_utils import plot_model\n",
    "from keras.layers import SimpleRNN,Dense,Activation\n",
    "from keras.optimizers import SGD,Adam\n",
    "from keras.preprocessing.image import ImageDataGenerator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据集的数量为： 35887\n"
     ]
    }
   ],
   "source": [
    "path = r\"D:\\AI\\fer2013\\fer2013.csv\"\n",
    "data = pd.read_csv(path)\n",
    "num_of_instances = len(data) #获取数据集的数量\n",
    "print(\"数据集的数量为：\",num_of_instances)\n",
    "\n",
    "pixels = data['pixels']\n",
    "emotions = data['emotion']\n",
    "usages = data['Usage']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_classes = 7   #表情的类别数目\n",
    "x_train,y_train,x_test,y_test = [],[],[],[]\n",
    "\n",
    "for emotion,img,usage in zip(emotions,pixels,usages):    \n",
    "    try: \n",
    "        emotion = keras.utils.to_categorical(emotion,num_classes)   # 独热向量编码\n",
    "        val = img.split(\" \")\n",
    "        pixels = np.array(val,'float32')\n",
    "        \n",
    "        if(usage == 'Training'):\n",
    "            x_train.append(pixels)\n",
    "            y_train.append(emotion)\n",
    "        elif(usage == 'PublicTest'):\n",
    "            x_test.append(pixels)\n",
    "            y_test.append(emotion)\n",
    "    except:\n",
    "        print(\"\",end=\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train = np.array(x_train)\n",
    "y_train = np.array(y_train)\n",
    "x_train = x_train.reshape(-1,48,48)\n",
    "x_test = np.array(x_test)\n",
    "y_test = np.array(y_test)\n",
    "x_test = x_test.reshape(-1,48,48)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/20\n",
      "28709/28709 [==============================] - 38s 1ms/step - loss: 1.8260 - acc: 0.2439\n",
      "Epoch 2/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7851 - acc: 0.2617\n",
      "Epoch 3/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7710 - acc: 0.2729\n",
      "Epoch 4/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7614 - acc: 0.2823\n",
      "Epoch 5/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7464 - acc: 0.2927\n",
      "Epoch 6/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7356 - acc: 0.2994\n",
      "Epoch 7/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7259 - acc: 0.3017\n",
      "Epoch 8/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7064 - acc: 0.3167\n",
      "Epoch 9/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.7017 - acc: 0.3223\n",
      "Epoch 10/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6999 - acc: 0.3182\n",
      "Epoch 11/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6818 - acc: 0.3317\n",
      "Epoch 12/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6743 - acc: 0.3380\n",
      "Epoch 13/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6663 - acc: 0.3435\n",
      "Epoch 14/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6562 - acc: 0.3495\n",
      "Epoch 15/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6478 - acc: 0.3514\n",
      "Epoch 16/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6467 - acc: 0.3523\n",
      "Epoch 17/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6292 - acc: 0.3632\n",
      "Epoch 18/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6312 - acc: 0.3583\n",
      "Epoch 19/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6200 - acc: 0.3644\n",
      "Epoch 20/20\n",
      "28709/28709 [==============================] - 33s 1ms/step - loss: 1.6144 - acc: 0.3700\n",
      "Train loss: 1.601281621637271\n",
      "Train accuracy: 37.7198787837086\n",
      "Test loss: 1.6168422882654567\n",
      "Test accuracy: 36.66759543131241\n"
     ]
    }
   ],
   "source": [
    "batch_size = 500\n",
    "epochs = 20\n",
    "\n",
    "model = Sequential()\n",
    "\n",
    "model.add(SimpleRNN(units = 1024,input_shape = (48,48),return_sequences = True,activation = 'relu'))\n",
    "model.add(SimpleRNN(units = 512,return_sequences = True,activation = 'tanh'))\n",
    "model.add(SimpleRNN(units = 256,return_sequences = True,activation = 'relu'))\n",
    "model.add(SimpleRNN(units = 128,return_sequences = True,activation = 'tanh'))\n",
    "model.add(SimpleRNN(units = 64,activation = 'relu'))\n",
    "model.add(Dense(7,activation='softmax'))\n",
    "\n",
    "adam = Adam(lr=1e-4)\n",
    "# adam = Adam()\n",
    "\n",
    "model.compile(loss = 'categorical_crossentropy',optimizer = adam,metrics=['accuracy'])\n",
    "model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs)\n",
    "\n",
    "train_score = model.evaluate(x_train, y_train, verbose=0)\n",
    "print('Train loss:', train_score[0])\n",
    "print('Train accuracy:', 100*train_score[1])\n",
    " \n",
    "test_score = model.evaluate(x_test, y_test, verbose=0)\n",
    "print('Test loss:', test_score[0])\n",
    "print('Test accuracy:', 100*test_score[1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
